package chj.Bookmanage;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;

import chj.Database.CDataBase;
import chj.bookface.BKJPanel;
import chj.BookInformation.Bookop;
//import connection;
import chj.Database.StringUtil;

public class Bfind {
    //书籍查询
    public static void BookLookFram() {

        JFrame frame = new JFrame("书籍查询");
        frame.setLayout(null);
        BKJPanel pan=new BKJPanel("src/chj/image/bg.jpg"); //背景图位置
        pan.setLayout(null);

        //开6个面板，方便设置位置
        JPanel pan1 = new JPanel();
        JPanel pan2 = new JPanel();
        JPanel pan3 = new JPanel();
        JPanel pan4 = new JPanel();
        JPanel pan8 = new JPanel();
        JPanel pan5 = new JPanel();//用来存放查询按钮
        JPanel pan7 = new JPanel();//用来存放查询全部按钮
        JPanel pan6 = new JPanel();//存放查询结果

        //文本框
        JTextField textbno = new JTextField();//
        JTextField textbname = new JTextField();//
        JTextField textbauthor = new JTextField();//
        JTextField textbcategory = new JTextField();
        //提示框
        JLabel label1 = new JLabel("书籍查询");
        JLabel label2 = new JLabel("书 籍 编 号  ");
        JLabel label3 = new JLabel("书 籍 名     ");
        JLabel label4 = new JLabel("作    者     ");
        JLabel label5 = new JLabel("书 籍 类 型  ");

        //按钮
        JButton button1 = new JButton("查询");
        JButton button2 = new JButton("全部查询");

        Font font = new Font("宋体",Font.BOLD,50);//标题字体大小
        Font f = new Font("宋体",Font.BOLD,25);//提示框字体大小
        Font f2 = new Font("宋体",Font.BOLD,13);//结果表中字体大小
        // 设置按钮样式
        button1.setBackground(new Color(30, 144, 255)); // 设置按钮背景色
        button1.setForeground(Color.WHITE); // 设置按钮文字颜色
        button1.setBorder(BorderFactory.createLineBorder(new Color(30, 144, 255))); // 设置边框颜色
        button1.setFocusPainted(false); // 去掉按钮聚焦时的边框
        button1.setOpaque(true);
        //button1.setBorderRadius(20); // 设置圆角

        // 设置按钮悬停效果
        button1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button1.setBackground(new Color(70, 130, 180)); // 悬停时改变背景色
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                button1.setBackground(new Color(30, 144, 255)); // 鼠标离开时恢复背景色
            }
        });

        // 设置第二个按钮的样式
        button2.setBackground(new Color(34, 139, 34)); // 设置按钮背景色
        button2.setForeground(Color.WHITE); // 设置按钮文字颜色
        button2.setBorder(BorderFactory.createLineBorder(new Color(34, 139, 34))); // 设置边框颜色
        button2.setFocusPainted(false); // 去掉按钮聚焦时的边框
        button2.setOpaque(true);
        //button2.setBorderRadius(20); // 设置圆角

        // 设置按钮悬停效果
        button2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button2.setBackground(new Color(50, 205, 50)); // 悬停时改变背景色
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                button2.setBackground(new Color(34, 139, 34)); // 鼠标离开时恢复背景色
            }
        });
        //设置文本框的大小
        textbno.setPreferredSize(new Dimension(200,30));
        textbname.setPreferredSize(new Dimension(200,30));
        textbauthor.setPreferredSize(new Dimension(200,30));
        textbcategory.setPreferredSize(new Dimension(200,30));
        button1.setPreferredSize(new Dimension(90,40));
        button2.setPreferredSize(new Dimension(300,40));

        //设置界面所有字体大小，包括标题、提示框字体和文本框
        label1.setFont(font);//设置标题字体
        label2.setFont(f);
        textbno.setFont(f);
        label3.setFont(f);
        textbname.setFont(f);
        label4.setFont(f);
        textbauthor.setFont(f);
        label5.setFont(f);
        textbcategory.setFont(f);
        button1.setFont(f);
        button2.setFont(f);

        //向面板中添加组件
        pan1.add(label1);
        pan2.add(label2);
        pan2.add(textbno);
        pan3.add(label3);
        pan3.add(textbname);
        pan4.add(label4);
        pan4.add(textbauthor);
        pan5.add(button1);
        pan7.add(button2);
        pan8.add(label5);
        pan8.add(textbcategory);

        //设置面板位置
        pan1.setOpaque(false);
        pan2.setOpaque(false);
        pan3.setOpaque(false);
        pan4.setOpaque(false);
        pan5.setOpaque(false);
        pan7.setOpaque(false);
        pan8.setOpaque(false);
        pan1.setBounds(80,50, 430, 60);
        pan2.setBounds(80,170, 430, 50);
        pan3.setBounds(80,240, 430, 50);
        pan4.setBounds(80,310, 430, 50);
        pan8.setBounds(80,380,430,50);
        pan5.setBounds(160,450,100, 50);//放查询按钮
        pan7.setBounds(340,450,100,50);
        String [] columnNames = {"编号","书籍名", "出版商", "作者", "类型","数量"};
        DefaultTableModel model = new DefaultTableModel(columnNames,22);
        JTable jTable = new JTable(model);
        // 设置表格的基本属性
        jTable.setFont(new Font("宋体", Font.PLAIN, 20)); // 设置表格字体，增加字体大小
        jTable.setRowHeight(30); // 设置行高，增大每行的高度

        // 设置表格背景色
        jTable.setBackground(new Color(245, 245, 245)); // 表格背景色
        jTable.setSelectionBackground(new Color(173, 216, 230)); // 选中行背景色
        jTable.setSelectionForeground(Color.BLACK); // 选中行文字颜色
        jTable.setGridColor(new Color(200, 200, 200)); // 设置网格线颜色

        // 设置表头样式
        JTableHeader header = jTable.getTableHeader();
        header.setFont(new Font("宋体", Font.BOLD, 18)); // 设置表头字体，增大字体大小
        header.setBackground(new Color(0, 120, 215)); // 表头背景色
        header.setForeground(Color.WHITE); // 表头字体颜色
        header.setReorderingAllowed(false); // 禁止列重新排序

        // 设置列宽和对齐方式
        TableColumnModel columnModel = jTable.getColumnModel();
        columnModel.getColumn(0).setPreferredWidth(120); // 书籍编号
        columnModel.getColumn(1).setPreferredWidth(180); // 书籍名
        columnModel.getColumn(2).setPreferredWidth(180); // 出版商
        columnModel.getColumn(3).setPreferredWidth(180); // 作者
        columnModel.getColumn(4).setPreferredWidth(180); // 书籍类型
        columnModel.getColumn(5).setPreferredWidth(120); // 书籍数量


        JScrollPane scrollPane = new JScrollPane(jTable);
        scrollPane.setBounds(500,80, 600, 450);

        //监听
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String bno = textbno.getText().toString();
                String bname = textbname.getText().toString();
                String bauthor = textbauthor.getText().toString();
                String bcategory = textbcategory.getText().toString();

                Connection con = null;
                CDataBase connection = new CDataBase();
                Bookop bookdao = new Bookop();
                Bookop bookMessage = new Bookop();

                bookMessage.setBno(bno);
                bookMessage.setBname(bname);
                bookMessage.setBauthor(bauthor);
                bookMessage.setBcategory(bcategory);

                DefaultTableModel dtm = (DefaultTableModel) jTable.getModel();
                dtm.setRowCount(0);//初始化为0行
                jTable.setFont(f2);
                try {
                    con = connection.getConnection();
                    ResultSet currentbook = bookdao.list(con,bookMessage);

                    while(currentbook.next()) {
                        Vector v = new Vector(10,0);
                        v.add(currentbook.getString("Bno"));
                        v.add(currentbook.getString("Bname"));
                        v.add(currentbook.getString("Bpress"));
                        v.add(currentbook.getString("Bauthor"));
                        v.add(currentbook.getString("Bcategory")); // 书籍类型列
                        v.add(currentbook.getString("Bnumber"));
                        dtm.addRow(v);
                    }

                }catch(Exception evt) {
                    evt.printStackTrace();
                }
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection con = null;
                CDataBase connection = new CDataBase();
                Bookop bookdao = new Bookop();

                // 清空表格，准备显示新的数据
                DefaultTableModel dtm = (DefaultTableModel) jTable.getModel();
                dtm.setRowCount(0);  // 清空当前表格数据
                jTable.setFont(f2);  // 设置表格字体

                try {
                    con = connection.getConnection();  // 获取数据库连接
                    ResultSet allBooks = bookdao.listAllBooks(con);  // 获取所有书籍

                    while (allBooks.next()) {
                        Vector v = new Vector(10, 0);
                        v.add(allBooks.getString("Bno"));  // 书籍编号
                        v.add(allBooks.getString("Bname"));  // 书籍名
                        v.add(allBooks.getString("Bpress"));  // 出版商
                        v.add(allBooks.getString("Bauthor"));  // 作者
                        v.add(allBooks.getString("Bcategory")); // 书籍类型
                        v.add(allBooks.getString("Bnumber"));

                        dtm.addRow(v);  // 将数据添加到表格
                    }

                } catch (Exception evt) {
                    evt.printStackTrace();  // 异常处理
                }
            }
        });


        //添加面板
        pan.add(pan1);
        pan.add(pan2);
        pan.add(pan3);
        pan.add(pan4);
        pan.add(pan5);
        pan.add(pan8);
        pan.add(pan6);
        pan.add(pan7);
        pan.add(scrollPane);
        frame.setContentPane(pan);

        //窗口设置
        frame.setBounds(300, 100, 1150, 650);
        frame.setVisible(true);
    }

}
